package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.dto.MoneyRecordImportDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.MoneyRecordMapper;
import com.ruoyi.system.domain.MoneyRecord;
import com.ruoyi.system.service.IMoneyRecordService;

/**
 * 钞币记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-02-21
 */
@Service
public class MoneyRecordServiceImpl implements IMoneyRecordService 
{
    @Autowired
    private MoneyRecordMapper moneyRecordMapper;

    /**
     * 查询钞币记录
     * 
     * @param moneyRecordId 钞币记录主键
     * @return 钞币记录
     */
    @Override
    public MoneyRecord selectMoneyRecordByMoneyRecordId(Long moneyRecordId)
    {
        return moneyRecordMapper.selectMoneyRecordByMoneyRecordId(moneyRecordId);
    }

    /**
     * 查询钞币记录列表
     * 
     * @param moneyRecord 钞币记录
     * @return 钞币记录
     */
    @Override
    public List<MoneyRecord> selectByMy(MoneyRecord moneyRecord)
    {
        moneyRecord.setCreatedBy(SecurityUtils.getUsername());
        return moneyRecordMapper.selectMoneyRecordList(moneyRecord);
    }

    /**
     * 新增钞币记录
     * 
     * @param moneyRecord 钞币记录
     * @return 结果
     */
    @Override
    public int insertMoneyRecord(MoneyRecord moneyRecord)
    {
        moneyRecord.setCreatedTime(new Date());
        moneyRecord.setCreatedBy(SecurityUtils.getUsername());
        return moneyRecordMapper.insertMoneyRecord(moneyRecord);
    }

    /**
     * 修改钞币记录
     * 
     * @param moneyRecord 钞币记录
     * @return 结果
     */
    @Override
    public int updateMoneyRecord(MoneyRecord moneyRecord)
    {
        return moneyRecordMapper.updateMoneyRecord(moneyRecord);
    }

    /**
     * 批量删除钞币记录
     * 
     * @param moneyRecordIds 需要删除的钞币记录主键
     * @return 结果
     */
    @Override
    public int deleteMoneyRecordByMoneyRecordIds(Long[] moneyRecordIds)
    {
        return moneyRecordMapper.deleteMoneyRecordByMoneyRecordIds(moneyRecordIds);
    }

    /**
     * 删除钞币记录信息
     * 
     * @param moneyRecordId 钞币记录主键
     * @return 结果
     */
    @Override
    public int deleteMoneyRecordByMoneyRecordId(Long moneyRecordId)
    {
        return moneyRecordMapper.deleteMoneyRecordByMoneyRecordId(moneyRecordId);
    }

    /**
     * 导入过滤数据库存在的数据
     * @param recordImportDtos
     * @return
     */
    @Override
    public List<MoneyRecordImportDto> filterRecord(List<MoneyRecordImportDto> recordImportDtos) {
        List<MoneyRecord> moneyRecords = moneyRecordMapper.selectMoneyRecordList(new MoneyRecord());

        //过滤出数据库不存在的数据
        return recordImportDtos.stream()
                .filter(recordImportDto -> moneyRecords.stream()
                        .noneMatch(moneyRecord ->
                                recordImportDto.getMoneyType().equals(moneyRecord.getMoneyType()) &&
                                        moneyRecord.getMoneyNumber().equals(recordImportDto.getMoneyNumber())  ))
                .collect(Collectors.toList());
    }

    @Override
    public int insertList(List<MoneyRecord> moneyRecords) {
        List<MoneyRecord> list = moneyRecordMapper.selectMoneyRecordList(new MoneyRecord());
        List<MoneyRecord> collect = moneyRecords.stream()
                .filter(recordImportDto -> list.stream()
                        .noneMatch(moneyRecord ->
                                recordImportDto.getMoneyType().equals(moneyRecord.getMoneyType()) &&
                                        moneyRecord.getMoneyNumber().equals(recordImportDto.getMoneyNumber())))
                .collect(Collectors.toList());
        if (collect.isEmpty()){
            throw new ServiceException("抱歉，没有适合导入的数据!");
        }
        collect.forEach(item ->{
            item.setCreatedBy(SecurityUtils.getUsername());
        });
        return moneyRecordMapper.insertList(collect);
    }

    @Override
    public List<MoneyRecord> selectList(MoneyRecord moneyRecord) {
        return moneyRecordMapper.selectMoneyRecordList(moneyRecord);
    }
}
